package com.seu.vcampus.view.library;

import com.seu.vcampus.client.VCampusClient;
import com.seu.vcampus.util.UIStyleUtil;
import com.seu.vcampus.vo.User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

/**
 * 借阅历史面板
 * 显示用户的借阅历史记录
 * 
 * @author 虚拟校园开发团队
 * @since 2.2.0
 */
public class BorrowHistoryPanel extends JPanel {
    private VCampusClient client;
    private User currentUser;
    
    private JTable historyTable;
    private DefaultTableModel historyTableModel;
    private JButton refreshButton;
    private JButton exportButton;
    
    public BorrowHistoryPanel(VCampusClient client, User user) {
        this.client = client;
        this.currentUser = user;
        
        initializeComponents();
        setupLayout();
        setupEventHandlers();
        loadHistory();
    }
    
    private void initializeComponents() {
        setLayout(new BorderLayout(15, 15));
        setBackground(UIStyleUtil.BACKGROUND_COLOR);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 初始化表格
        String[] columns = {"记录ID", "图书ID", "书名", "借阅日期", "归还日期", "状态"};
        historyTableModel = new DefaultTableModel(columns, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        historyTable = new JTable(historyTableModel);
        historyTable.setFont(UIStyleUtil.BODY_FONT);
        historyTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
        historyTable.setRowHeight(30);
        
        // 按钮
        refreshButton = UIStyleUtil.createSecondaryButton("刷新");
        exportButton = UIStyleUtil.createSuccessButton("导出");
    }
    
    private void setupLayout() {
        // 按钮面板
        JPanel buttonPanel = UIStyleUtil.createCardPanel("操作");
        buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
        buttonPanel.add(refreshButton);
        buttonPanel.add(exportButton);
        
        // 表格面板
        JPanel tablePanel = UIStyleUtil.createCardPanel("借阅历史");
        tablePanel.setLayout(new BorderLayout());
        JScrollPane scrollPane = UIStyleUtil.createModernScrollPane(historyTable);
        tablePanel.add(scrollPane, BorderLayout.CENTER);
        
        add(buttonPanel, BorderLayout.NORTH);
        add(tablePanel, BorderLayout.CENTER);
    }
    
    private void setupEventHandlers() {
        refreshButton.addActionListener(e -> loadHistory());
        exportButton.addActionListener(e -> exportHistory());
    }
    
    private void loadHistory() {
        // 实现加载用户借阅历史的逻辑
        // 这里可以调用服务端接口获取用户的历史借阅记录
    }
    
    private void exportHistory() {
        // 实现导出借阅历史的逻辑
        JOptionPane.showMessageDialog(this, 
            "借阅历史导出功能开发中...", 
            "提示", JOptionPane.INFORMATION_MESSAGE);
    }
    
    public void refreshData() {
        loadHistory();
    }
}